package stackOverflow;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author Boris Cherkasky
 *
 */
public class CodeObject {
    private ArrayList<String>	code;
    private long		creationDate;		//creation timestamp
    private int			userRep;		//user reputation
    private int			upVoteCount;		//number of up votes
    private int 		downVoteCount;		//number of down votes
    private int			id;			//answer or question id
    private int 		score;			//answer or question score
    private int 		viewCount;		//answer or question view count
    private long		lastActivityDate;	//last modification timestamp
    private String 		userDisplayName;	//user display name
    //when data is loaded from dump - dates are Strings!
    private String		lastActivityDateString;	//last activity date as string (from dump)
    private String 		creationDateString;	//createion date as string (from dump)
    
    public CodeObject(ArrayList<String> code, int date, int userRep,
	    int upVoteCount, int id) {
	super();
	this.code 		= code == null ? new ArrayList<String>() : new ArrayList<String>(code);
	this.creationDate 	= date;
	this.userRep 		= userRep;
	this.upVoteCount 	= upVoteCount;
	this.id 		= id;
    }
    
    public CodeObject(String body, int date, int userRep, int upVoteCount,
	    int id) {
	super();
	ArrayList<String> tmpCode = new ArrayList<String>();
	if (body != null && !body.equals("")){
	    tmpCode.add(body);
	}
	this.code = tmpCode;
	this.creationDate = date;
	this.userRep = userRep;
	this.upVoteCount = upVoteCount;
	this.id = id;
    }

    public CodeObject() {
	super();
	this.code 	  	= new ArrayList<String>();
	this.creationDate	= 0;
	this.userRep 		= 0;
	this.upVoteCount	= 0;
	this.id 		= 0;
	this.score 		= 0;
	this.viewCount 		= 0;
	this.lastActivityDate 	= 0;
	this.userDisplayName 	= "";
	this.creationDateString = null;
	this.lastActivityDateString = null;
    }

    public int getDownVoteCount(){ 
         return downVoteCount;
    }

    public void setDownVoteCount(int downVoteCount) {
        this.downVoteCount = downVoteCount;
    }
    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getViewCount() {
        return viewCount;
    }

    public void setViewCount(int viewCount) {
        this.viewCount = viewCount;
    }

    public long getLastActivityDate() {
        return lastActivityDate;
    }

    public void setLastActivityDate(long lastActivity) {
        this.lastActivityDate = lastActivity;
    }

    public String getUserDisplayName() {
        return userDisplayName;
    }

    public void setUserDisplayName(String userDisplayName) {
        this.userDisplayName = userDisplayName;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }    
    public ArrayList<String> getCode() {
        return code;
    }
    public void setCode(ArrayList<String> code) {
        this.code = code == null ? new ArrayList<String>() : new ArrayList<String>(code);
    }
    public void setCode(String code) {
        this.code.add(code);
    }
    public long getCreationDate() {
        return creationDate;
    }
    public void setCreationDate(long date) {
        this.creationDate = date;
    }
    public int getUserRep() {
        return userRep;
    }
    public void setUserRep(int userRep) {
        this.userRep = userRep;
    }
    public int getUpVoteCount() {
        return upVoteCount;
    }
    public void setUpVoteCount(int upVoteCount) {
        this.upVoteCount = upVoteCount;
    }
    
    public String getLastActivityDateString() {
        return lastActivityDateString;
    }

    public void setLastActivityDateString(String lastActivityDateString) {
        this.lastActivityDateString = lastActivityDateString;
    }

    public String getCreationDateString() {
        return creationDateString;
    }

    public void setCreationDateString(String creationDateString) {
        this.creationDateString = creationDateString;
    }

    public void showMeta(){
	if (this instanceof AnswerObject){
	    System.out.println("/****************Answer****************/");
	} else {
	    System.out.println("/****************Question****************/");
	}
	
	System.out.println("/*\t|-------------------\n");
	System.out.println("\t|ID		: \t" + id + "\n");
	Date d;
	if (creationDateString == null){
	    d = new Date(creationDate*1000);
	    System.out.println("\t|Creation Date 	: \t" + d + "\n");
	} else {
	    System.out.println("\t|Creation Date 	: \t" + creationDateString + "\n");
	}
	if (lastActivityDateString == null){
	    d = new Date(lastActivityDate*1000);
	    System.out.println("\t|Last Activity Date: \t" + d + "\n");
	} else{
	    System.out.println("\t|Last Activity Date: \t" + lastActivityDateString + "\n");
	}
	
	System.out.println("\t|DisplayName 	: \t" + userDisplayName + "\n");
	System.out.println("\t|Reputation  	: \t" + userRep + "\n");
	System.out.println("\t|View Count  	: \t" + viewCount + "\n");
	System.out.println("\t|Up Votes	  	: \t" + upVoteCount + "\n");
	System.out.println("\t|Down Votes  	: \t" + downVoteCount + "\n");
	System.out.println("\t|Score	  	: \t" + score + "\n");
	System.out.println("\t|-------------------*/\n");
    }
    
    public void showMeta(FileWriter writer) throws IOException{
	if (writer == null){
	    this.showMeta();
	} else {
	    if (this instanceof AnswerObject){
		writer.write("/****************Answer****************/\n");
	    } else{
		writer.write("/****************Question****************/\n");
	    }
	    writer.write("/*\t|-------------------\n");
	    writer.write("\t|ID		: \t" + id + "\n");
	    Date d;
	    if (creationDateString == null){
		d = new Date(creationDate*1000);
		writer.write("\t|Creation Date 	: \t" + d + "\n");
	    } else {
		writer.write("\t|Creation Date 	: \t" + creationDateString + "\n");
	    }
	    if (lastActivityDateString == null){
		d = new Date(lastActivityDate*1000);
		writer.write("\t|Last Activity Date: \t" + d + "\n");
	    } else{
		writer.write("\t|Last Activity Date: \t" + lastActivityDateString + "\n");
	    }
	    writer.write("\t|DisplayName 	: \t" + userDisplayName + "\n");
	    writer.write("\t|Reputation  	: \t" + userRep + "\n");
	    writer.write("\t|View Count  	: \t" + viewCount + "\n");
	    writer.write("\t|Up Votes	  	: \t" + upVoteCount + "\n");
	    writer.write("\t|Down Votes  	: \t" + downVoteCount + "\n");
	    writer.write("\t|Score	  	: \t" + score + "\n");
	    writer.write("\t|-------------------*/\n");
	}
    }
}
